####### 最终生产版：修复了所有已知问题 #######

FROM golang:1.17.5-alpine3.15

ARG CACHE_BUSTER
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# (关键修复) 安装 go get 命令所必需的 git 工具。
RUN apk add --no-cache git curl python2 build-base openssl-dev openssl tzdata

# (关键修复) 设置容器的时区为东八区，解决日志时间戳相差8小时的问题。
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# --- 缓存重量级依赖 ---
RUN curl https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-367.0.0-linux-x86_64.tar.gz --output gcloud.tar.gz \
    && tar -xf gcloud.tar.gz -C / \
    && rm -f gcloud.tar.gz \
    && /google-cloud-sdk/bin/gcloud components install app-engine-python-extras app-engine-python --quiet
ENV STUNNEL_VERSION 5.60
RUN curl https://www.stunnel.org/archive/5.x/stunnel-${STUNNEL_VERSION}.tar.gz --output /tmp/stunnel.tar.gz\
    && tar -xf /tmp/stunnel.tar.gz -C /tmp \
    && cd /tmp/stunnel-${STUNNEL_VERSION} \
    && ./configure --prefix=/usr && make && make install \
    && rm -rf /tmp/stunnel*

# --- 克隆并构建代码 ---
RUN echo "Cache buster: ${CACHE_BUSTER}" && git clone https://gitee.com/damn_code/apprtc-server.git /apprtc
WORKDIR /apprtc

ENV GOPATH /go
ENV GO111MODULE off
# (关键修复) 在编译前，强制、显式地安装最关键的 websocket 依赖。
RUN go get -v golang.org/x/net/websocket

RUN mkdir -p $GOPATH/src \
    && cp -r src/collider/collider $GOPATH/src/ \
    && cp -r src/collider/collidermain $GOPATH/src/ \
    && cp -r src/collider/collidertest $GOPATH/src/ \
    && cd $GOPATH/src/collidermain \
    && go install .

RUN python build/build_app_engine_package.py src/ out/
RUN curl -L https://webrtc.github.io/adapter/adapter-latest.js --output src/web_app/js/adapter.js
RUN cp src/web_app/js/*.js out/js/

# --- 配置和启动 ---
RUN mkdir /cert
RUN openssl req -x509 -out /cert/cert.crt -keyout /cert/key.pem \
  -newkey rsa:2048 -nodes -sha256 \
  -subj '/CN=appr.tc' -extensions EXT -config <( \
   printf "[dn]\nCN=appr.tc\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:appr.tc\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth") \
  && cat /cert/key.pem > /cert/cert.pem \
  && cat /cert/cert.crt >> /cert/cert.pem \
  && chmod 600 /cert/cert.pem /cert/key.pem /cert/cert.crt

RUN echo 'foreground=yes' > /usr/etc/stunnel/stunnel.conf \
    && echo '[AppRTC GAE]' >> /usr/etc/stunnel/stunnel.conf \
    && echo 'accept=0.0.0.0:443' >> /usr/etc/stunnel/stunnel.conf \
    && echo 'connect=0.0.0.0:8080' >> /usr/etc/stunnel/stunnel.conf \
    && echo 'cert=/cert/cert.pem' >> /usr/etc/stunnel/stunnel.conf

# 恢复为正常的、所有服务都在后台运行的启动脚本。
RUN echo '#!/bin/sh' > /go/start.sh \
    && echo '/google-cloud-sdk/bin/dev_appserver.py --host 0.0.0.0 --addn_host appr.tc /apprtc/out/app.yaml &' >> /go/start.sh \
    && echo '/go/bin/collidermain -port=8089 -tls=false -room-server=http://localhost:8080 &' >> /go/start.sh \
    && echo '/usr/bin/stunnel &' >> /go/start.sh \
    && echo 'wait -n' >> /go/start.sh \
    && echo 'exit $?' >> /go/start.sh \
    && chmod +x /go/start.sh

CMD /go/start.sh